package DT3;

import javagprssrv.Log;
import java.net.DatagramSocket;
import javagprssrv.Common.CommonClass;
import java.net.InetAddress;
import java.net.DatagramPacket;
import java.util.Date;
import javagprssrv.Common.CRC;
import javagprssrv.Common.DateTime;
import javagprssrv.StructClass.structIATask;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structRecvData;

public class TrdUDPDataTrans extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String AsynTelNo;
    java.lang.String TaskID;
    
    public TrdUDPDataTrans(final javagprssrv.StructClass.structRecvData _recvData) {
        super("TrdUDPDataTrans");
        this.JobIndex = -1;
        this.RecvDataType = -1;
        final int firstChar = _recvData.Data.substring(0, 1).charAt(0);
        if (firstChar != 89 && firstChar != 65) {
            _recvData.Data = "A" + _recvData.Data;
        }
        this.recvData = _recvData;
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._UDPServerSocketIndex = this.recvData.UDPServerSocketIndex;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
    }
    
    @java.lang.Override
    public void run() {
        try {
            this.UDPDataSend(java.lang.String.valueOf('\u008f') + java.lang.String.valueOf('\u008f') + java.lang.String.valueOf('\u008f') + java.lang.String.valueOf('\u008f'));
            java.lang.Thread.sleep(1000L);
            final int Data_0 = this._Data.charAt(0);
            final int Data_2 = this._Data.charAt(4);
            if (Data_0 == 89) {
                this.do_59();
            }
            if (Data_0 == 65) {
                this.locateTask();
                boolean isJSSJ = false;
                try {
                    isJSSJ = this.task.Task_isJSSJ;
                }
                catch (java.lang.Exception ex) {}
                if (isJSSJ) {
                    synchronized (javagprssrv.JGS_Param.IATasks) {
                        for (int idx = javagprssrv.JGS_Param.IATasks.size() - 1; idx >= 0; --idx) {
                            if (((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.task.JSSJ_ClientIP) && ((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.task.JSSJ_ClientPort) {
                            	((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).RecvData = this._Data;
                                break;
                            }
                        }
                    }
                }
                else {
                    synchronized (javagprssrv.JGS_Param.UnSavedRecvData_UDP) {
                        javagprssrv.JGS_Param.UnSavedRecvData_UDP.add(this.recvData);
                    }
                }
                final int cmd = this._Data.substring(4, 5).charAt(0);
                if (cmd == 243) {
                    return;
                }
                if (cmd == 244) {
                    return;
                }
                if (cmd == 245) {
                    return;
                }
                this.do_41();
            }
        }
        catch (java.lang.Exception ex2) {}
    }
    
    private void locateTask() {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    break;
                }
            }
        }
    }
    
    private void do_41() throws java.lang.Exception {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    if (this.JobIndex >= javagprssrv.JGS_Param.Tasks.elementAt(i).Task_Jobs.size()) {
                        javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = "2000-01-01 01:02:03";
                        break;
                    }
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_59() throws java.lang.Exception {
        this.AsynTelNo = this._Data.substring(1, 12);
        int idxTask = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 1) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).TaskAsynTelNO.equals(this.AsynTelNo)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP = this.recvData.RemoteIP;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port = java.lang.String.valueOf(this.recvData.RemotePort);
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State = 2;
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_nextJob() throws java.lang.Exception {
        java.lang.String job = this.task.Task_Jobs.elementAt(this.JobIndex).trim().toUpperCase();
        job += "                ";
        if (job.substring(0, 9).equals("GET DATE ")) {
            this.do_getDate(job);
        }
        if (job.substring(0, 9).equals("SET DATE ")) {
            this.do_setDate(job);
        }
        if (job.substring(0, 10).equals("GET MONTH ")) {
            this.do_getMonth(job);
        }
        if (job.substring(0, 10).equals("SET MONTH ")) {
            this.do_setMonth(job);
        }
        if (job.substring(0, 8).equals("GET TIME")) {
            this.do_getTime(job);
        }
        if (job.substring(0, 8).equals("SET TIME")) {
            this.do_setTime(job);
        }
        if (job.substring(0, 9).equals("GET PARAM")) {
            this.do_getParam(job);
        }
        if (job.substring(0, 9).equals("SET PARAM")) {
            this.do_setParam(job);
        }
        if (job.substring(0, 7).equals("GET SXX")) {
            this.do_getSXX(job);
        }
        if (job.substring(0, 7).equals("SET SXX")) {
            this.do_setSXX(job);
        }
    }
    
    private void do_getTime(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 8).equals("GET TIME")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString = cmdString + '\r' + "0000Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_setTime(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET TIME ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        if (job.substring(9, 21).toUpperCase().equals("$SERVERTIME$")) {
            cmdString = cmdString + '-' + "0000" + new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(2) + "Z";
        }
        else {
            cmdString = cmdString + '-' + "0000" + job.substring(9, 21) + "Z";
        }
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_getParam(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET PARAM")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString = cmdString + '\u000e' + "0000Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_getSXX(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 7).equals("GET SXX")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString = cmdString + '\u000e' + "0000Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_setSXX(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 7).equals("SET SXX")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString = cmdString + '.' + "0000" + job.trim().substring(8) + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_setParam(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET PARAM ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString = cmdString + '.' + "0000" + job.trim().substring(10) + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_getDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET DATE ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        final int month = java.lang.Integer.valueOf(job.substring(9, 11));
        final int date = java.lang.Integer.valueOf(job.substring(11, 13));
        final java.util.Date dt = new java.util.Date();
        final int currMonth = dt.getMonth() + 1;
        java.lang.String cmd = "";
        if (month == currMonth) {
            cmd = java.lang.String.valueOf('\u0004');
        }
        else {
            cmd = java.lang.String.valueOf('\u0005');
        }
        cmdString += cmd;
        if (date >= 10) {
            cmdString += java.lang.String.valueOf(date);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(date);
        }
        cmdString += "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_setDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET DATE ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        final int month = java.lang.Integer.valueOf(job.substring(9, 11));
        final int date = java.lang.Integer.valueOf(job.substring(11, 13));
        final java.util.Date dt = new java.util.Date();
        final int currMonth = dt.getMonth() + 1;
        java.lang.String cmd = "";
        if (month == currMonth) {
            cmd = java.lang.String.valueOf('$');
        }
        else {
            cmd = java.lang.String.valueOf('%');
        }
        cmdString += cmd;
        if (date >= 10) {
            cmdString += java.lang.String.valueOf(date);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(date);
        }
        cmdString = cmdString + "00" + new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(2, 4) + job.substring(9) + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_getMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET MONTH ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        final java.lang.String cmd = java.lang.String.valueOf('\u0003');
        cmdString += cmd;
        final int month = java.lang.Integer.valueOf(job.substring(10, 12));
        if (month >= 10) {
            cmdString += java.lang.String.valueOf(month);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(month);
        }
        cmdString += "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void do_setMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET MONTH ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString += '#';
        cmdString = cmdString + job.substring(10, 12) + "00";
        cmdString += new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4);
        cmdString = cmdString + job.substring(10, 12) + "01";
        cmdString += job.substring(12);
        java.lang.String s = "";
        for (int i = 0; i < 96; ++i) {
            s += "0";
        }
        cmdString = cmdString + s + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.UDPDataSend(cmdString);
    }
    
    private void UDPDataSend(final java.lang.String strSend) {
        try {
            final java.net.DatagramPacket DP = new java.net.DatagramPacket(strSend.getBytes(), strSend.getBytes().length);
            DP.setAddress(java.net.InetAddress.getByName(this._RemoteIP));
            DP.setPort(this._RemotePort);
            final byte[] buf = javagprssrv.Common.CommonClass.String2ByteArray(strSend);
            DP.setData(buf, 0, strSend.length());
            final boolean isSendUDPDataMustByServerSocket = true;
            if (isSendUDPDataMustByServerSocket) {
            	((DatagramSocket)javagprssrv.JGS_Param.UDPServerSockets.elementAt(this._UDPServerSocketIndex)).send(DP);
            }
            javagprssrv.JGS_Param.myLog.ShowLog("[P=UDP,T=SND " + java.lang.String.valueOf(strSend.length()) + ",D=" + this._RemoteIP + ":" + java.lang.String.valueOf(this._RemotePort) + "]" + new javagprssrv.Log().String2HexString(strSend, " "), true, true);
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}
